<?php
/**
 * Dachengpay支付.
 */

namespace app\api\service\payment;

use app\common\library\exception\ParameterException;

use app\api\service\ApiPayment;
use app\common\library\exception\OrderException;
use think\Log;
use PhpOffice\PhpSpreadsheet\Exception;

class Dachengpay extends ApiPayment
{
    /**
     * 扫码
     */
    public function qr($order)
    {
        // $url = 'http://admin.v1k4ga.cn:16000/pay/qr/page';

        // $url = 'https://user.wenzipay.cn/pay/qr/page';
        $url = 'http://api.wenzipay.cn/pay/qr/page';

        return $this->requestApi($url, $order);
    }

    public function requestApi($url, $order)
    {
        $params = array(
            'outTradeNo' => $order['trade_no'],
            'amount' => $order['amount'],
            'notifyUrl' => "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}/notify/Dachengpay",
            'appId' => $this->config['appId'],
            'merchantId' => $this->config['merchantId'],
        );

        $signature = $this->makeSign($params, $this->config['key']);
        $params['sign'] = $signature;
        $res = $this->http_post_request($url, http_build_query($params));
        $resArr = json_decode($res, true);
        if (empty($resArr['payLink'])) {
            throw new ParameterException([
                'msg'   => 'Channel API Error:[Request CreatePaymentAndWaitEscrow Fail].'
            ]);
        }

        return [
            'order_id' => $order['out_trade_no'],
            'pay_seq' => $order['trade_no'],
            'pay_url' => $resArr['payLink'],
        ];
    }

    public function notify()
    {
        Log::write("Dachengpay notify Request:\n\nURL: ".var_export($_REQUEST, true));

        return $this->verifyOrderNotify();
    }

    /**
     * 回调验签.
     *
     *
     * @return array
     *
     * @throws OrderException
     */
    private function verifyOrderNotify()
    {
        return array(
            'trade_no' => $_REQUEST['outTradeNo'],
        );
    }

    public function makeSign($params, $key)
    {
        ksort($params);
        $uri = urldecode(http_build_query($params));
        $uri = $uri . $key;
        $result = md5($uri);
        return $result;
    }


    public function http_post_request($url, $params, $header = null)
    {
        $curl = curl_init($url);
        if ($header) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
        $res = curl_exec($curl);
        // curl失败
        if (0 !== curl_errno($curl)) {
            print_r(curl_error($curl));
            curl_close($curl);

            return false;
        }
        // curl成功
        curl_close($curl);
        return $res;
    }

    /**
     * 回调成功参数
     */
    public function callbackSuccessResponse()
    {
        return "0000";
    }
}
